package pe.edu.upc.xcode.food.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;

import pe.edu.upc.xcode.food.domain.Categoriaplato;
import pe.edu.upc.xcode.food.domain.Plato;
import pe.edu.upc.xcode.food.domain.Restaurant;


@Controller
public class PlatoController {
	
	@Autowired
	private RestTemplate restTemplate;
	
	private Gson gson = new Gson();
	 
	@RequestMapping("/plato/ingresar.htm")
	public ModelAndView ingresar(HttpServletRequest request,
		HttpServletResponse response) throws Exception {		
		return new ModelAndView("plato/lisPlato");
	}
	
	@RequestMapping("/plato/ingresarEdit.htm")
	public ModelAndView ingresarEdit(HttpServletRequest request,
		HttpServletResponse response) throws Exception {		
		return new ModelAndView("plato/ediPlato");
	}

	
	@RequestMapping("/plato/ingresarSave.htm")
	public ModelAndView saveRegistro(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		
		
		
		return new ModelAndView("plato/regPlato");
	}
	
	@RequestMapping("/plato/save.htm")
	public ModelAndView save(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		
		String BASE_URL = "http://localhost:8090/FoodService/services/rest/platos/";
		
		String codigoRestaurant = request.getParameter("txtRestaurant")!=null ? request.getParameter("txtRestaurant") : "";
		String nombrePlato = request.getParameter("txtNombrePlato")!=null ? request.getParameter("txtNombrePlato") : "";
		String categoriaPlato = request.getParameter("txtCategoria")!=null ? request.getParameter("txtCategoria") : "";
		String descripcionPlato = request.getParameter("txaDescripcion")!=null ? request.getParameter("txaDescripcion") : "";
		String fechaPublicacion = request.getParameter("txtFecPubli")!=null ? request.getParameter("txtFecPubli") : "";
		
		Restaurant r= new Restaurant();
		r.setCodigo(Integer.parseInt(codigoRestaurant));
		Categoriaplato cp = new Categoriaplato();
		cp.setCodigo((Integer.parseInt(categoriaPlato)));
		
		SimpleDateFormat format =
	            new SimpleDateFormat("yyyy-MM-dd");
		
		Plato p = new Plato(r,cp,nombrePlato, descripcionPlato, new Date() , 
				new Date(), "A");
		
		String json = gson.toJson(p);
	    restTemplate.postForObject(BASE_URL, json, String.class);
		
		return new ModelAndView("plato/lisPlato");		
	}

	
	@RequestMapping("/login/initial.htm")
	public ModelAndView registrar(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		
		//platoWS.listar();
		
		return new ModelAndView("login/login");
	}
	
	@RequestMapping("/plato/update.htm")
	public ModelAndView update(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		
		String BASE_URL = "http://localhost:8090/FoodService/services/rest/platos/update";
		
		String codigoRestaurant = request.getParameter("txtRestaurant")!=null ? request.getParameter("txtRestaurant") : "";
		String nombrePlato = request.getParameter("txtNombrePlato")!=null ? request.getParameter("txtNombrePlato") : "";
		String categoriaPlato = request.getParameter("txtCategoria")!=null ? request.getParameter("txtCategoria") : "";
		String descripcionPlato = request.getParameter("txaDescripcion")!=null ? request.getParameter("txaDescripcion") : "";
		String fechaPublicacion = request.getParameter("txtFecPubli")!=null ? request.getParameter("txtFecPubli") : "";
		
		Restaurant r= new Restaurant();
		r.setCodigo(Integer.parseInt(codigoRestaurant));
		Categoriaplato cp = new Categoriaplato();
		cp.setCodigo((Integer.parseInt(categoriaPlato)));
		
		SimpleDateFormat format =
	            new SimpleDateFormat("yyyy-MM-dd");
		
		Plato p = new Plato(r,cp,nombrePlato, descripcionPlato, new Date() , 
				new Date(), "A");
		
		String json = gson.toJson(p);
	    restTemplate.postForObject(BASE_URL, json, String.class);
		
		return new ModelAndView("plato/lisPlato");
	}
	
	@RequestMapping("/plato/delete.htm")
	public ModelAndView delete(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		
		String BASE_URL = "http://localhost:8090/FoodService/services/rest/platos/delete";
		
		String codigoRestaurant = request.getParameter("txtRestaurant")!=null ? request.getParameter("txtRestaurant") : "";
		String nombrePlato = request.getParameter("txtNombrePlato")!=null ? request.getParameter("txtNombrePlato") : "";
		String categoriaPlato = request.getParameter("txtCategoria")!=null ? request.getParameter("txtCategoria") : "";
		String descripcionPlato = request.getParameter("txaDescripcion")!=null ? request.getParameter("txaDescripcion") : "";
		String fechaPublicacion = request.getParameter("txtFecPubli")!=null ? request.getParameter("txtFecPubli") : "";
		
		Restaurant r= new Restaurant();
		r.setCodigo(Integer.parseInt(codigoRestaurant));
		Categoriaplato cp = new Categoriaplato();
		cp.setCodigo((Integer.parseInt(categoriaPlato)));
		
		SimpleDateFormat format =
	            new SimpleDateFormat("yyyy-MM-dd");
		
		Plato p = new Plato(r,cp,nombrePlato, descripcionPlato, new Date() , 
				new Date(), "A");
		
		String json = gson.toJson(p);
	    restTemplate.postForObject(BASE_URL, json, String.class);
		
		return new ModelAndView("plato/lisPlato");
	}
	
}
